package cn.edu.neusoft.mall.controller;

import cn.edu.neusoft.framework.Exption.MD5Util;
import cn.edu.neusoft.framework.Exption.ServiceException;
import cn.edu.neusoft.framework.annotations.Controller;
import cn.edu.neusoft.framework.annotations.RequestMapping;
import cn.edu.neusoft.framework.annotations.RequestParam;
import cn.edu.neusoft.framework.model.ModelAndView;
import cn.edu.neusoft.mall.Dao.UserDao;
import cn.edu.neusoft.mall.Dao.impl.UserDaoImpl;
import cn.edu.neusoft.mall.Utils.constant;
import cn.edu.neusoft.mall.entity.User;
import cn.edu.neusoft.mall.entity.subscribe;
import cn.edu.neusoft.mall.service.impl.petServiceimpl;
import cn.edu.neusoft.mall.service.impl.subServiceimpl;
import cn.edu.neusoft.mall.service.impl.userServiceimpl;
import cn.edu.neusoft.mall.service.petService;
import cn.edu.neusoft.mall.service.userService;
import cn.edu.neusoft.mall.service.subService;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
public class userController {
    userService userService = new userServiceimpl();
    petService petService = new petServiceimpl();
    subService subService = new subServiceimpl();
    @RequestMapping("/login")
    public ModelAndView login(@RequestParam(name = "username") String username, @RequestParam(name = "password") String password) {
        ModelAndView modelAndView = new ModelAndView("user/login");
        return modelAndView;
    }

    @RequestMapping("/zhuce")
    public ModelAndView zhuce() {
        ModelAndView modelAndView = new ModelAndView("user/zhuce");
        return modelAndView;
    }

    @RequestMapping("/zhuce-complete")
    public ModelAndView zhuce(User user) {


        User userVerify = userService.getUserByNum(user);

        if (userVerify != null) {
            ModelAndView mv = new ModelAndView("index");
            mv.addObject("loginSuccess", true);
            mv.addObject("message", "注册成功！");
            mv.addObject("index_petsList", petService.getpetList());
            return mv;
        } else {
            ModelAndView mv = new ModelAndView("user/zhuce");
            mv.addObject("loginSuccess", false);
            mv.addObject("message", "注册失败，昵称已存在或者输入违规！！");
            return mv;
        }

    }

    @RequestMapping("/success")
    public ModelAndView success() {
        ModelAndView modelAndView = new ModelAndView("user/success");
        return modelAndView;
    }

    @RequestMapping("/loginin")
    public ModelAndView loginin(User user, HttpSession httpSession) {
        User user1 = userService.insertUser(user);
        if (user1 != null && !user1.getUsername().equals("admin")) {
            ModelAndView mv = new ModelAndView("index");
            httpSession.setAttribute(constant.login_user, user1);
            mv.addObject("loginSuccess", true);
            mv.addObject("message", "登录成功！");
            mv.addObject("index_petsList", petService.getpetList());
            return mv;
        }else if(user.getUsername().equals("admin") && user1.getPasswordSecret().equals(MD5Util.md5(user.getPassword()))){
            return new ModelAndView("admin/contion");
        }else {
            ModelAndView mv = new ModelAndView("user/zhuce");
            return mv;
        }
    }

    @RequestMapping("/quituser")
    public ModelAndView quituser(HttpSession httpSession) {
        ModelAndView modelAndView = new ModelAndView("user/lose");
        httpSession.setAttribute(constant.login_user, null);
        return modelAndView;
    }

    @RequestMapping("/userdetails1")
    public ModelAndView userdetails1() {
        ModelAndView modelAndView = new ModelAndView("user/userDetails");
        return modelAndView;
    }

    @RequestMapping("/userdetails")
    public ModelAndView userdetails(HttpSession httpSession, User user) {
        User user1 = (User) httpSession.getAttribute(constant.login_user);
        String MD5password = MD5Util.md5(user.getPassword());
        user.setPasswordSecret(MD5password);
        int bool = userService.updateuserde(user, user1);
        if (bool == 1) {
            ModelAndView mv = new ModelAndView("index");
            mv.addObject("loginSuccess", false);
            mv.addObject("message", "修改成功！");
            mv.addObject("index_petsList", petService.getpetList());
            return mv;
        } else {
            ModelAndView mv = new ModelAndView("user/userDetails");
            return mv;
        }
    }

    @RequestMapping("/myLove")
    public ModelAndView myLove(HttpSession httpSession, @RequestParam(name = "lovePage", defaultValue = "1") int lovePage) {
        ModelAndView modelAndView = new ModelAndView("user/myLove");
        User user1 = (User) httpSession.getAttribute(constant.login_user);
        modelAndView.addObject("loveDogs", petService.getLovepetdog1(user1.getId()));
        modelAndView.addObject("loveCats", petService.getLovepetcat1(user1.getId()));
        int count = petService.getLovepetdog1(user1.getId()).size() + petService.getLovepetcat1(user1.getId()).size();
        modelAndView.addObject("countPets", count);
        modelAndView.addObject("countPetpage", lovePage);
        return modelAndView;
    }

    @RequestMapping("/userSubscribe")
    public ModelAndView userSubscribe(HttpSession httpSession) {
        ModelAndView modelAndView = new ModelAndView("user/userSubscribe");
        User user1 = (User) httpSession.getAttribute(constant.login_user);
        modelAndView.addObject("subDogs", petService.getsubpetdog1(user1.getId()));
        modelAndView.addObject("subCats", petService.getsubpetcat1(user1.getId()));
        int count = petService.getsubpetdog1(user1.getId()).size() + petService.getsubpetcat1(user1.getId()).size();
        modelAndView.addObject("countPets", count);
        return modelAndView;
    }
    @RequestMapping("/addsub")
    public String addsub(HttpSession httpSession,@RequestParam(name = "pet_id", defaultValue = "1") String pet_id,@RequestParam(name = "appointmentTime", defaultValue = "1") Date dataTime) {
        User user1 = (User) httpSession.getAttribute(constant.login_user);
        subscribe sub=new subscribe();
        sub.setTime(dataTime);
        sub.setUser_id(user1.getId());
        sub.setPet_id(pet_id);
        int a=subService.addSubService(sub);
        if(a==1){
            petService.setsubDog(pet_id);
            petService.setsubcat(pet_id);
            return "redirect:userSubscribe.action";
        }else{
            throw new ServiceException("预约失败");
        }
    }
}